<?php
namespace mybackend\controllers\ext;

use common\services\outstock\FlowCenterService;
use common\services\outstock\DeliveryOrderFlowService;
use yii\log\Logger;


/**
 * Created by PhpStorm.
 * User: silenceper
 * Date: 2017/5/8
 * Time: 上午11:50
 */
class FlowCenterController extends BaseSignController
{
    /**
     * 租户添加
     * @throws \yii\base\InvalidConfigException
     */
    public function actionAddOrg()
    {
        //post
        $orgId = \Yii::$app->request->post("orgId");
        $orderCode = \Yii::$app->request->post("orderCode");
        $orgName = \Yii::$app->request->post("orgName");
        $orgCode = \Yii::$app->request->post("orgCode");
        $password = \Yii::$app->request->post("password");
        $userId = \Yii::$app->request->post("userId");

        //must 
        if (empty($orgId) || empty($orderCode) || empty($orgName) || empty($orgCode) || empty($password) || empty($userId)) {
            $this->exportJson([], -1, "参数必填");
        }
        if(substr($orderCode,0,1)=='S'){
            //新出库流程，以S作为标识
            /* @var DeliveryOrderFlowService $deliveryOrderFlowService */
            $deliveryOrderFlowService = \Yii::createObject(DeliveryOrderFlowService::className());
            $ret = $deliveryOrderFlowService->createOrg($orgId, $orderCode, $orgName, $orgCode, $password, $userId);
            if (false === $ret) {
                $this->log("deliveryOrderFlowService::createOrg 失败,orderCode:$orderCode", Logger::LEVEL_ERROR);
            }
            $this->exportJson([], $deliveryOrderFlowService->getErrCode(), $deliveryOrderFlowService->getErrMsg());
        }else{
            //保留原来的出库流程
            /* @var $flowCenterService FlowCenterService */
            $flowCenterService = \Yii::createObject(FlowCenterService::className());
            $ret = $flowCenterService->addOrg($orgId, $orderCode, $orgName, $orgCode, $password, $userId);
            if (false === $ret) {
                $this->log("FlowCenterService::addOrg 失败,orderCode:$orderCode", Logger::LEVEL_ERROR);
            }
            $this->exportJson([], $flowCenterService->getErrCode(), $flowCenterService->getErrMsg());
        }
    }

    /**
     * 添加授权
     * @throws \yii\base\InvalidConfigException
     */
    public function actionAddAuthContract()
    {
        $orderCode = \Yii::$app->request->post("orderCode");
        $contractCode = \Yii::$app->request->post("contractCode");
        $userId = \Yii::$app->request->post("userId");
        $beginOn = \Yii::$app->request->request('beginOn');
        $endOn = \Yii::$app->request->request('endOn');
        if(substr($orderCode,0,1)=='S'){
            //新出库流程，以S作为标识
            /* @var DeliveryOrderFlowService $deliveryOrderFlowService */
            $deliveryOrderFlowService = \Yii::createObject(DeliveryOrderFlowService::className());
            $ret = $deliveryOrderFlowService->orderSQ($orderCode, $contractCode, $userId, $beginOn, $endOn);
            if (false === $ret) {
                $this->log("deliveryOrderFlowService::orderSQ 失败,orderCode:$orderCode", Logger::LEVEL_ERROR);
            }
            $this->exportJson([], $deliveryOrderFlowService->getErrCode(), $deliveryOrderFlowService->getErrMsg());

        }else{
            //保留原来的出库流程
            /* @var $flowCenterService FlowCenterService */
            $flowCenterService = \Yii::createObject(FlowCenterService::className());
            $ret = $flowCenterService->addAuthContract($orderCode, $contractCode, $userId, $beginOn, $endOn);
            if (false === $ret) {
                $this->log("FlowCenterService::addAuthContract 失败,orderCode:$orderCode,contractCode:$contractCode,errMsg:" . $flowCenterService->getErrMsg(), Logger::LEVEL_ERROR);
            }
            $this->exportJson([], $flowCenterService->getErrCode(), $flowCenterService->getErrMsg());

        }
       }
    /**
     * 发送通知邮件
     * @throws \yii\base\InvalidConfigException
     */
    public function actionSendNoticeEmail()
    {
        $orderCode = \Yii::$app->request->post("orderCode");
        $action = \Yii::$app->request->request('action');
        /* @var DeliveryOrderFlowService $deliveryOrderFlowService */
        $deliveryOrderFlowService = \Yii::createObject(DeliveryOrderFlowService::className());
        $ret=false;
        if($action=='sq') {
            $contractCode = \Yii::$app->request->post("contractCode");
            $beginOn = \Yii::$app->request->request('beginOn');
            $endOn = \Yii::$app->request->request('endOn');
            $ret = $deliveryOrderFlowService->sendSqEmail($orderCode, $contractCode, $beginOn, $endOn);
        }else if($action=='kt') {
            $orgName = \Yii::$app->request->post("orgName");
            $orgCode = \Yii::$app->request->post("orgCode");
            $password = \Yii::$app->request->post("password");
            $ret = $deliveryOrderFlowService->sendKtEmail($orderCode, $orgName, $orgCode, $password);
        }
        if (false === $ret) {
            $this->log("deliveryOrderFlowService::sendNoticeEmail 失败,orderCode:$orderCode,action:$action", Logger::LEVEL_ERROR);
        }
        $this->exportJson([], $deliveryOrderFlowService->getErrCode(), $deliveryOrderFlowService->getErrMsg());
    }
}